package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.common.*;
import com.navinfo.tripanalysis.common.arithmetic.common.OutStatisticsRangeData.RangeAcceleratedSpeedStatisticsData;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarRangeSpeed;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarRangeSpeedTest;
import com.navinfo.tripanalysis.common.arithmetic.enums.RangeAcceleratedSpeedEnum;
import com.navinfo.tripanalysis.common.arithmetic.enums.RangeSpeedEnum;
import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.config.ConfigMap;
import com.navinfo.tripanalysis.common.util.ArithmeticUtils;

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;

import static com.navinfo.tripanalysis.common.arithmetic.enums.EventStatus.IN;

/**
 * 速度区间统计算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 依赖{@link IdleParking}的怠速状态<br/>
 * 输出：统计
 * @author 沈东生
 */
public class RangeSpeedTest extends AbstractArithmetic<VarRangeSpeedTest> {
    /**
     * 有效车速时长比例阀值，根据这个统计最高最低有效车速 单位: 0-1 之间的double值
     */
    private static final double usefulPercent = ConfigMap.getDouble("SpeedRange_useful_percent_threshold");
    /**
     * 时长阀值 单位：ms
     */
    private int timeThreshold =  ConfigMap.getInteger("GearSlide_Time_Threshold");
    /**
     * 速度阀值 单位：km/h
     */
    private int speedThreshold = ConfigMap.getInteger("GearSlide_Speed_Threshold");
    /**
     *  转数阀值 单位：RPM
     */
    private int rotationThreshold = ConfigMap.getInteger("GearSlide_Rotation_Threshold");

    /**
     * 判断当前点是否为在档滑行状态
     * 在档滑行状态判定:速度>30km/h,发动机转速>=800rpm，不踩油门，燃油消耗率为0】
     */
    public boolean isCurrentGearSlide() {
        CommonData data = getCommonData();
        return data.getSpeedKmh()>speedThreshold  && data.getRotation()>=rotationThreshold && data.getAcceleratorPosition()==0.00;
    }

    @Override
    public void calculate() {
        logger.debug("进入速度区间统计算法.[terminalId:{}]", terminalId);
        VarRangeSpeedTest var = getObj();

        boolean previousIdle = var.isPreGearSlide();
        boolean currentIdle = isCurrentGearSlide();
        var.setPreGearSlide(currentIdle);

        CommonData commonData = getCommonData();
        int speed = commonData.getSpeedKmh();
        int dfftime = commonData.getDffTime();
        double dffMileage = commonData.getDffMileage();
        double dffFuel = commonData.getDffFuel();
        //新增油门开度和转速
        int rotation = commonData.getRotation();
        double acceleratorPosition = commonData.getAcceleratorPosition();

        //增加加速度,扩大10倍
        int acceleratedSpeed = (int)Math.ceil(ArithmeticUtils.calculateAcceleratedSpeed(
                commonData.getLastSpeedMs().doubleValue(),
                ArithmeticUtils.speedKmhToMs(speed),dfftime)*10);

        if (speed<0) {
            logger.error("速度区间统计算法,速度值不合法，不进行统计：speedKmh:{}", speed);
            return;
        }

        //统计急减速里程
        if(acceleratedSpeed < -7 && commonData.isBrake()){
            double mileage = var.getSharpDeceleration() + dffMileage;
            var.setSharpDeceleration(ArithmeticUtils.mileageMToM(mileage));
        }

        VarNumberStatistics varSpeed = null;
        VarRangeSpeedTest.UsefulSpeed usefulSpeed = var.getUsefulSpeed();
        if (RangeSpeedEnum.RANGE_10.getRange().contains(speed)) {
            varSpeed = var.getSpeed10();
            usefulSpeed.setUsefulSpeed10Min(speed);
            usefulSpeed.setUsefulSpeed10Max(speed);
        }else if(RangeSpeedEnum.RANGE_20.getRange().contains(speed)){
            varSpeed = var.getSpeed20();
            usefulSpeed.setUsefulSpeed20Min(speed);
            usefulSpeed.setUsefulSpeed20Max(speed);
        }else if(RangeSpeedEnum.RANGE_30.getRange().contains(speed)){
            varSpeed = var.getSpeed30();
            usefulSpeed.setUsefulSpeed30Min(speed);
            usefulSpeed.setUsefulSpeed30Max(speed);
        }else if(RangeSpeedEnum.RANGE_40.getRange().contains(speed)){
            varSpeed = var.getSpeed40();
            usefulSpeed.setUsefulSpeed40Min(speed);
            usefulSpeed.setUsefulSpeed40Max(speed);
        }else if(RangeSpeedEnum.RANGE_50.getRange().contains(speed)) {
            varSpeed = var.getSpeed50();
            usefulSpeed.setUsefulSpeed50Min(speed);
            usefulSpeed.setUsefulSpeed50Max(speed);
        }else if(RangeSpeedEnum.RANGE_60.getRange().contains(speed)) {
            varSpeed = var.getSpeed60();
            usefulSpeed.setUsefulSpeed60Min(speed);
            usefulSpeed.setUsefulSpeed60Max(speed);
        }else if(RangeSpeedEnum.RANGE_70.getRange().contains(speed)) {
            varSpeed = var.getSpeed70();
            usefulSpeed.setUsefulSpeed70Min(speed);
            usefulSpeed.setUsefulSpeed70Max(speed);
        }else if(RangeSpeedEnum.RANGE_80.getRange().contains(speed)) {
            varSpeed = var.getSpeed80();
            usefulSpeed.setUsefulSpeed80Min(speed);
            usefulSpeed.setUsefulSpeed80Max(speed);
        }else if(RangeSpeedEnum.RANGE_90.getRange().contains(speed)) {
            varSpeed = var.getSpeed90();
            usefulSpeed.setUsefulSpeed90Min(speed);
            usefulSpeed.setUsefulSpeed90Max(speed);
        }else if(RangeSpeedEnum.RANGE_100.getRange().contains(speed)) {
            varSpeed = var.getSpeed100();
            usefulSpeed.setUsefulSpeed100Min(speed);
            usefulSpeed.setUsefulSpeed100Max(speed);
        }else if(RangeSpeedEnum.RANGE_110.getRange().contains(speed)) {
            varSpeed = var.getSpeed110();
            usefulSpeed.setUsefulSpeed110Min(speed);
            usefulSpeed.setUsefulSpeed110Max(speed);
        }else if(RangeSpeedEnum.RANGE_120.getRange().contains(speed)) {
            varSpeed = var.getSpeed120();
            usefulSpeed.setUsefulSpeed120Min(speed);
            usefulSpeed.setUsefulSpeed120Max(speed);
        }else if(RangeSpeedEnum.RANGE_OVER_120.getRange().contains(speed)) {
            varSpeed = var.getSpeedOver120();
            usefulSpeed.setUsefulSpeedOver120Min(speed);
            usefulSpeed.setUsefulSpeedOver120Max(speed);
        }

        if(null != varSpeed){
            varSpeed.addMileage(dffMileage);
            varSpeed.addFuel(dffFuel);
            varSpeed.addDuration(dfftime);
            varSpeed.addNumber(1);
            varSpeed.addRotation(rotation);
            varSpeed.addGasOpenPercent(acceleratorPosition);

            switch (getEventStatus(previousIdle, currentIdle)) {
                case START:
                    var.setCostInGearStartTime(commonData.getGpsTime());
                    break;
                case IN:
                    varSpeed.addGearInMileage(dffMileage);
                    //行程结束时要清理对应的未结束事件
                    if (getCommonData().isTripLastNode()) {
                        if(commonData.getGpsTime() - var.getCostInGearStartTime() >= timeThreshold){
                            var.addGearMileage();
                        }
                        var.clearGearInMileage();
                        var.setCostInGearStartTime(0L);
                    }
                    break;
                case END:
                    varSpeed.addGearInMileage(dffMileage);
                    if(commonData.getGpsTime() - var.getCostInGearStartTime() >= timeThreshold){
                        var.addGearMileage();
                    }
                    var.clearGearInMileage();
                    var.setCostInGearStartTime(0L);
                    break;
                default:
                    break;
            }
        }

        //非怠速状态下的最小速度
        if(commonData.isWork()) {
            var.setMinSpeed(var.getMinSpeed()==null ? speed : (var.getMinSpeed()>speed ? speed : var.getMinSpeed()));
            var.setMaxSpeed(var.getMaxSpeed()==null ? speed : (var.getMaxSpeed()<speed ? speed : var.getMaxSpeed()));
        }


        //累加加速度里程、油耗、时长
        VarCommon accSpeed = null;
        if(RangeAcceleratedSpeedEnum.Range_F10.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeedF1();
        }else if(RangeAcceleratedSpeedEnum.Range_F7.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeedF07();
        }else if(RangeAcceleratedSpeedEnum.Range_F5.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeedF05();
        }else if(RangeAcceleratedSpeedEnum.Range_F3.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeedF03();
        }else if(RangeAcceleratedSpeedEnum.Range_F1.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeedF01();
        }else if(RangeAcceleratedSpeedEnum.Range_0.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeed0();
        }else if(RangeAcceleratedSpeedEnum.Range_eq_0.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeedEq0();
        }else if(RangeAcceleratedSpeedEnum.Range_1.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeed01();
        }else if(RangeAcceleratedSpeedEnum.Range_3.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeed03();
        }else if(RangeAcceleratedSpeedEnum.Range_5.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeed05();
        }else if(RangeAcceleratedSpeedEnum.Range_7.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeed07();
        }else if(RangeAcceleratedSpeedEnum.Range_10.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeed1();
        }else if(RangeAcceleratedSpeedEnum.Range_Max.getRange().contains(acceleratedSpeed)){
            accSpeed = var.getAcceleratedSpeedMax();
        }

        if(null != accSpeed){
            accSpeed.addMileage(dffMileage);
            accSpeed.addFuel(dffFuel);
            accSpeed.addDuration(dfftime);
        }

        logger.debug("速度区间统计算法结束.[terminalId:{}]", terminalId);
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeSpeedTest var = getObj();
        statisticDataOut.setSpeedRange01Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed10().getFuel()));
        statisticDataOut.setSpeedRange01Mileage(ArithmeticUtils.mileageMToM(var.getSpeed10().getMileage()));
        statisticDataOut.setSpeedRange01Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed10().getDuration()));
        statisticDataOut.setSpeedRange01Rotation(var.getSpeed10().getRotation());
        statisticDataOut.setSpeedRange01GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed10().getGasOpenPercent()));
        statisticDataOut.setSpeedRange01GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed10().getGearMileage()));
        statisticDataOut.setSpeedRange01Number(var.getSpeed10().getNumber());

        statisticDataOut.setSpeedRange02Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed20().getFuel()));
        statisticDataOut.setSpeedRange02Mileage(ArithmeticUtils.mileageMToM(var.getSpeed20().getMileage()));
        statisticDataOut.setSpeedRange02Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed20().getDuration()));
        statisticDataOut.setSpeedRange02Rotation(var.getSpeed20().getRotation());
        statisticDataOut.setSpeedRange02GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed20().getGasOpenPercent()));
        statisticDataOut.setSpeedRange02GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed20().getGearMileage()));
        statisticDataOut.setSpeedRange02Number(var.getSpeed20().getNumber());

        statisticDataOut.setSpeedRange03Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed30().getFuel()));
        statisticDataOut.setSpeedRange03Mileage(ArithmeticUtils.mileageMToM(var.getSpeed30().getMileage()));
        statisticDataOut.setSpeedRange03Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed30().getDuration()));
        statisticDataOut.setSpeedRange03Rotation(var.getSpeed30().getRotation());
        statisticDataOut.setSpeedRange03GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed30().getGasOpenPercent()));
        statisticDataOut.setSpeedRange03GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed30().getGearMileage()));
        statisticDataOut.setSpeedRange03Number(var.getSpeed30().getNumber());

        statisticDataOut.setSpeedRange04Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed40().getFuel()));
        statisticDataOut.setSpeedRange04Mileage(ArithmeticUtils.mileageMToM(var.getSpeed40().getMileage()));
        statisticDataOut.setSpeedRange04Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed40().getDuration()));
        statisticDataOut.setSpeedRange04Rotation(var.getSpeed40().getRotation());
        statisticDataOut.setSpeedRange04GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed40().getGasOpenPercent()));
        statisticDataOut.setSpeedRange04GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed40().getGearMileage()));
        statisticDataOut.setSpeedRange04Number(var.getSpeed40().getNumber());

        statisticDataOut.setSpeedRange05Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed50().getFuel()));
        statisticDataOut.setSpeedRange05Mileage(ArithmeticUtils.mileageMToM(var.getSpeed50().getMileage()));
        statisticDataOut.setSpeedRange05Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed50().getDuration()));
        statisticDataOut.setSpeedRange05Rotation(var.getSpeed50().getRotation());
        statisticDataOut.setSpeedRange05GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed50().getGasOpenPercent()));
        statisticDataOut.setSpeedRange05GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed50().getGearMileage()));
        statisticDataOut.setSpeedRange05Number(var.getSpeed50().getNumber());

        statisticDataOut.setSpeedRange06Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed60().getFuel()));
        statisticDataOut.setSpeedRange06Mileage(ArithmeticUtils.mileageMToM(var.getSpeed60().getMileage()));
        statisticDataOut.setSpeedRange06Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed60().getDuration()));
        statisticDataOut.setSpeedRange06Rotation(var.getSpeed60().getRotation());
        statisticDataOut.setSpeedRange06GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed60().getGasOpenPercent()));
        statisticDataOut.setSpeedRange06GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed60().getGearMileage()));
        statisticDataOut.setSpeedRange06Number(var.getSpeed60().getNumber());

        statisticDataOut.setSpeedRange07Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed70().getFuel()));
        statisticDataOut.setSpeedRange07Mileage(ArithmeticUtils.mileageMToM(var.getSpeed70().getMileage()));
        statisticDataOut.setSpeedRange07Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed70().getDuration()));
        statisticDataOut.setSpeedRange07Rotation(var.getSpeed70().getRotation());
        statisticDataOut.setSpeedRange07GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed70().getGasOpenPercent()));
        statisticDataOut.setSpeedRange07GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed70().getGearMileage()));
        statisticDataOut.setSpeedRange07Number(var.getSpeed70().getNumber());

        statisticDataOut.setSpeedRange08Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed80().getFuel()));
        statisticDataOut.setSpeedRange08Mileage(ArithmeticUtils.mileageMToM(var.getSpeed80().getMileage()));
        statisticDataOut.setSpeedRange08Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed80().getDuration()));
        statisticDataOut.setSpeedRange08Rotation(var.getSpeed80().getRotation());
        statisticDataOut.setSpeedRange08GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed80().getGasOpenPercent()));
        statisticDataOut.setSpeedRange08GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed80().getGearMileage()));
        statisticDataOut.setSpeedRange08Number(var.getSpeed80().getNumber());

        statisticDataOut.setSpeedRange09Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed90().getFuel()));
        statisticDataOut.setSpeedRange09Mileage(ArithmeticUtils.mileageMToM(var.getSpeed90().getMileage()));
        statisticDataOut.setSpeedRange09Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed90().getDuration()));
        statisticDataOut.setSpeedRange09Rotation(var.getSpeed90().getRotation());
        statisticDataOut.setSpeedRange09GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed90().getGasOpenPercent()));
        statisticDataOut.setSpeedRange09GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed90().getGearMileage()));
        statisticDataOut.setSpeedRange09Number(var.getSpeed90().getNumber());

        statisticDataOut.setSpeedRange10Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed100().getFuel()));
        statisticDataOut.setSpeedRange10Mileage(ArithmeticUtils.mileageMToM(var.getSpeed100().getMileage()));
        statisticDataOut.setSpeedRange10Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed100().getDuration()));
        statisticDataOut.setSpeedRange10Rotation(var.getSpeed100().getRotation());
        statisticDataOut.setSpeedRange10GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed100().getGasOpenPercent()));
        statisticDataOut.setSpeedRange10GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed100().getGearMileage()));
        statisticDataOut.setSpeedRange10Number(var.getSpeed100().getNumber());

        statisticDataOut.setSpeedRange11Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed110().getFuel()));
        statisticDataOut.setSpeedRange11Mileage(ArithmeticUtils.mileageMToM(var.getSpeed110().getMileage()));
        statisticDataOut.setSpeedRange11Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed110().getDuration()));
        statisticDataOut.setSpeedRange11Rotation(var.getSpeed110().getRotation());
        statisticDataOut.setSpeedRange11GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed110().getGasOpenPercent()));
        statisticDataOut.setSpeedRange11GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed110().getGearMileage()));
        statisticDataOut.setSpeedRange11Number(var.getSpeed110().getNumber());

        statisticDataOut.setSpeedRange12Fuel(ArithmeticUtils.fuelMlToMl(var.getSpeed120().getFuel()));
        statisticDataOut.setSpeedRange12Mileage(ArithmeticUtils.mileageMToM(var.getSpeed120().getMileage()));
        statisticDataOut.setSpeedRange12Duration(ArithmeticUtils.timeMsToSecond(var.getSpeed120().getDuration()));
        statisticDataOut.setSpeedRange12Rotation(var.getSpeed120().getRotation());
        statisticDataOut.setSpeedRange12GasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeed120().getGasOpenPercent()));
        statisticDataOut.setSpeedRange12GearMileage(ArithmeticUtils.mileageMToM(var.getSpeed120().getGearMileage()));
        statisticDataOut.setSpeedRange12Number(var.getSpeed120().getNumber());

        statisticDataOut.setExceedSpeedRangeFuel(ArithmeticUtils.fuelMlToMl(var.getSpeedOver120().getFuel()));
        statisticDataOut.setExceedSpeedRangeMileage(ArithmeticUtils.mileageMToM(var.getSpeedOver120().getMileage()));
        statisticDataOut.setExceedSpeedRangeDuration(ArithmeticUtils.timeMsToSecond(var.getSpeedOver120().getDuration()));
        statisticDataOut.setExceedSpeedRangeRotation(var.getSpeedOver120().getRotation());
        statisticDataOut.setExceedSpeedRangeGasOpenPercent(ArithmeticUtils.gasOpenPercentToInt(var.getSpeedOver120().getGasOpenPercent()));
        statisticDataOut.setExceedSpeedRangeGearMileage(ArithmeticUtils.mileageMToM(var.getSpeedOver120().getGearMileage()));
        statisticDataOut.setExceedSpeedRangeNumber(var.getSpeedOver120().getNumber());

        logger.error("======速度区间带档滑行里程:{}",statisticDataOut.getSpeedRange01GearMileage()+
                statisticDataOut.getSpeedRange02GearMileage()+
                statisticDataOut.getSpeedRange03GearMileage()+
                statisticDataOut.getSpeedRange04GearMileage()+
                statisticDataOut.getSpeedRange05GearMileage()+
                statisticDataOut.getSpeedRange06GearMileage()+
                statisticDataOut.getSpeedRange07GearMileage()+
                statisticDataOut.getSpeedRange08GearMileage()+
                statisticDataOut.getSpeedRange09GearMileage()+
                statisticDataOut.getSpeedRange10GearMileage()+
                statisticDataOut.getSpeedRange11GearMileage()+
                statisticDataOut.getSpeedRange12GearMileage()+
                statisticDataOut.getExceedSpeedRangeGearMileage());
        //最大最小速度
        statisticDataOut.setMinSpeed(var.getMinSpeed());
        statisticDataOut.setMaxSpeed(var.getMaxSpeed());

        //求最大最小有效速度
        VarRangeSpeedTest.UsefulSpeed usefulSpeed = var.getUsefulSpeed();
        double workDuration = getCommonData().getIntegralDuration();
        if (workDuration > 0) {
            //有效速度列表
            List<Integer> usefulSpeedList = new ArrayList<>();

            if (var.getSpeed10().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed10Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed10Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed20().getDuration()/workDuration  > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed20Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed20Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed30().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed30Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed30Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed40().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed40Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed40Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed50().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed50Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed50Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed60().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed60Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed60Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed70().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed70Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed70Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed80().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed80Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed80Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed90().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed90Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed90Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed100().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed100Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed100Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed110().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed110Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed110Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeed120().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeed120Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeed120Max()).ifPresent(usefulSpeedList::add);
            }
            if (var.getSpeedOver120().getDuration()/workDuration > usefulPercent) {
                Optional.ofNullable(usefulSpeed.getSpeedOver120Min()).ifPresent(usefulSpeedList::add);
                Optional.ofNullable(usefulSpeed.getSpeedOver120Max()).ifPresent(usefulSpeedList::add);
            }

            //设置有效的最大最小速度
            if (usefulSpeedList.size()>0) {
                IntSummaryStatistics intSummaryStatistics = usefulSpeedList.stream().mapToInt(Integer::intValue).summaryStatistics();
                statisticDataOut.setMinUsefulSpeed(intSummaryStatistics.getMin());
                statisticDataOut.setMaxUsefulSpeed(intSummaryStatistics.getMax());
            }
        } else {
            logger.error("engineWorkDuration<=0");
        }

        //加速度统计
        RangeAcceleratedSpeedStatisticsData rangeAcceleratedSpeedStatisticsData = new RangeAcceleratedSpeedStatisticsData();
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF1_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeedF1().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF1_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeedF1().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF1_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeedF1().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF07_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeedF07().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF07_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeedF07().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF07_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeedF07().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF05_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeedF05().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF05_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeedF05().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF05_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeedF05().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF03_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeedF03().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF03_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeedF03().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF03_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeedF03().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF01_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeedF01().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF01_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeedF01().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeF01_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeedF01().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange0_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeed0().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange0_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeed0().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange0_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeed0().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeEq0_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeedEq0().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeEq0_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeedEq0().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeEq0_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeedEq0().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange01_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeed01().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange01_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeed01().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange01_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeed01().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange03_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeed03().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange03_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeed03().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange03_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeed03().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange05_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeed05().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange05_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeed05().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange05_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeed05().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange07_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeed07().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange07_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeed07().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange07_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeed07().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange1_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeed1().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange1_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeed1().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRange1_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeed1().getDuration()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeMax_mileage(ArithmeticUtils.mileageMToM(var.getAcceleratedSpeedMax().getMileage()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeMax_fuel(ArithmeticUtils.fuelMlToMl(var.getAcceleratedSpeedMax().getFuel()));
        rangeAcceleratedSpeedStatisticsData.setAcceleratedSpeedRangeMax_duration(ArithmeticUtils.timeMsToSecond(var.getAcceleratedSpeedMax().getDuration()));
        statisticDataOut.setRangeAcceleratedSpeedStatisticsData(rangeAcceleratedSpeedStatisticsData);

        //统计急减速里程
        statisticDataOut.setSharpDeceleration(var.getSharpDeceleration());
    }
}
